/**
 * Tested using data from link: http://astro.temple.edu/~dhill001/course/numanalspring2010/Hermite%20Interpolation%20Section%205_7.pdf
 * 
 * 
 * 
 * 
 * 
 */
package testCode;

import java.util.Arrays;

import interfaces.ILinearEquation;

import org.apache.commons.math3.analysis.polynomials.PolynomialFunction;

import tools.LinearEquationToolkit_v1;
import tools.PolynomialGenerator;
import tools.UtilFunctions;

/**
 * @author Vivek
 * 
 */
public class Test2DPolynomial {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		double[][] xy = {{0.4,-0.24738092489897845},{0.8,-0.17323801040687792},{1.2000000000000002,-0.17309483060237008},{1.225,-0.1699057083720648},{1.2500000000000002,-0.1659807028902327},{1.2750000000000001,-0.16125952329127807},{1.3000000000000003,-0.15568187870960282},{1.3250000000000002,-0.14918747827961037},{1.35,-0.14171603113570364},{1.3750000000000002,-0.13320724641228543},{1.4000000000000001,-0.12360083324375881},{1.425,-0.11283650076452689},{1.4500000000000002,-0.10085395810899245},{1.475,-0.08759291441155848},{1.5,-0.07299307880662809},{1.5250000000000001,-0.05699416042860406},{1.55,-0.039535868411889385},{1.5750000000000002,-0.02055791189088707},{1.6,0.0}};
/*
 * 4 Patterns
 */
//		double[][] xy = {{0.4,-0.33302749023226397,-0.33302749023226397,-0.45714522896011556,0.5428547710398844},{0.8,-0.2192810815398979,-0.33302749023226397,-0.3214365743643972,0.5428547710398844},{1.2000000000000002,-0.2194523264861422,-0.33302749023226397,-0.32165472840357545,0.5428547710398844},{1.6,0.0,0.0,0.0,0.0}}
//		double[][] xy = {{0.4,-0.2501110514634307,-0.37222505159869057,-0.36000625172014344,0.5},{0.8,-0.2192810815398979,-0.33302749023226397,-0.3214365743643972,0.5428547710398844},{1.2000000000000002,-0.21910993271005608,-0.3328054071515584,-0.3212184982199683,0.5431029238532721},{1.6,0.0,0.0,0.0,0.0}}
//		double[][] xy = {{0.4,-0.2501110514634307,-0.37222505159869057,-0.36000625172014344,0.5},{0.8,-0.2192810815398979,-0.33302749023226397,-0.3214365743643972,0.5428547710398844},{1.2000000000000002,-0.2194523264861422,-0.3332496474888874,-0.32165472840357545,0.5426065969565229},{1.6,0.0,0.0,0.0,0.0}}

		
		// ILinearEquation lEq = new LinearEquationToolkit_v1();
		PolynomialGenerator lEq = new PolynomialGenerator(xy);

		// System.out.println(lEq.coefficients.toString());

		lEq.solveEquation();
		// System.out.println(lEq.constants.toString());
//		System.out.println(lEq.constants.toString());


		PolynomialFunction[] polys = new PolynomialFunction[xy[0].length-1];



		polys[0] = new PolynomialFunction(lEq.solution.toArray());
		System.out.println(polys[0].toString());
//		System.out.println(polys[0].value(x));
		for (int n = 1; n < polys.length; n++) {
			polys[n] = (PolynomialFunction) polys[n - 1].derivative();
			// System.out.println(polys[n]);
//			System.out.println(polys[n].value(x));
		}
		
		for(int sample = 0; sample < xy.length; sample++){
			for(int derivates = 0; derivates < polys.length; derivates++){	
//				System.out.println("Run for :"+xy[sample][0]);
//				System.out.println("Expected :"+xy[sample][derivates+1]);
//				System.out.println("Returned :"+polys[derivates].value(xy[sample][0]));
				if (xy[sample][derivates+1] != 0){
					System.out.println("Run for :"+xy[sample][0]+ " Percent error :"+ (((xy[sample][derivates+1]- polys[derivates].value(xy[sample][0]))/xy[sample][derivates+1])*100));
				}else{
					System.out.println("Run for :"+xy[sample][0]+ " Percent error :"+ (polys[derivates].value(xy[sample][0])));
				}
			}
		}

	}

}
